/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.sys.service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moon.admin.framework.secure.AuthUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moon.admin.sys.entity.SysMenuEntity;
import com.moon.admin.sys.mapper.SysMenuMapper;

/**
 * 菜单服务
 * @author ninglong
 */
@Service
public class SysMenuService extends ServiceImpl<SysMenuMapper, SysMenuEntity>{
	
	@Autowired
	private SysModuleService moduleService;
	@Autowired
	private SysUserService userService;
	
	/**
	 * 菜单列表
	 * 包括菜单和按扭
	 * @param hasBtn 是否包含按扭
	 */
	public List<SysMenuEntity> getSysMenuList(boolean hasBtn) {
		LambdaQueryWrapper<SysMenuEntity> lqw = Wrappers.<SysMenuEntity>lambdaQuery();
		if (!hasBtn) {
			lqw.ne(SysMenuEntity::getMenuType, "F");
		}
		lqw.orderByAsc(SysMenuEntity::getParentId,SysMenuEntity::getOrderNum);
		List<SysMenuEntity> menus = this.list(lqw);
		return menus;
	}

	/**
	 * 获取一个用户的菜单树
	 * @param userId
	 * @return
	 */
	public List<SysMenuEntity> selectMenuTreeByUserId(Integer userId) {
		List<SysMenuEntity> menus = null;
		if (AuthUtils.isAdministration()) {
			menus = this.list(Wrappers.<SysMenuEntity>lambdaQuery().in(SysMenuEntity::getMenuType,'M','C').orderByAsc(SysMenuEntity::getParentId,SysMenuEntity::getOrderNum));
		} else {
			menus = this.baseMapper.getMenuListByUserId(userId);
		}
		return getChildPerms(menus, 0);
	}
	/**
	 * 根据父节点的ID获取所有子节点
	 *
	 * @param list 分类表
	 * @param parentId 传入的父节点ID
	 * @return String
	 */
	private List<SysMenuEntity> getChildPerms(List<SysMenuEntity> list, int parentId) {
		List<SysMenuEntity> returnList = new ArrayList<SysMenuEntity>();
		for (Iterator<SysMenuEntity> iterator = list.iterator(); iterator.hasNext();) {
			SysMenuEntity t = (SysMenuEntity) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentId() == parentId)
			{
				recursionFn(list, t);
				returnList.add(t);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 *
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<SysMenuEntity> list, SysMenuEntity t) {
		// 得到子节点列表
		List<SysMenuEntity> childList = getChildList(list, t);
		t.setChildren(childList);
		for (SysMenuEntity tChild : childList) {
			if (hasChild(list, tChild))
			{
				recursionFn(list, tChild);
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<SysMenuEntity> getChildList(List<SysMenuEntity> list, SysMenuEntity t) {
		List<SysMenuEntity> tlist = new ArrayList<SysMenuEntity>();
		Iterator<SysMenuEntity> it = list.iterator();
		while (it.hasNext()) {
			SysMenuEntity n = (SysMenuEntity) it.next();
			if (n.getParentId().intValue() == t.getId().intValue())
			{
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<SysMenuEntity> list, SysMenuEntity t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

	/**
	 * 获取一个用户拥有的权限
	 */
	public Set<String> getMenuPermsByUserId(Integer userId) {
		Set<String> permsSet = new HashSet<>();
		if (AuthUtils.isAdministration()) {
			permsSet.add("*_*_*");
			return  permsSet;
		}
		List<SysMenuEntity> sysMenus = this.baseMapper.getMenuListByUserId(userId);
		for (SysMenuEntity sysMenu : sysMenus) {
			if (StringUtils.isNotEmpty(sysMenu.getPerms())) {
				permsSet.addAll(Arrays.asList(sysMenu.getPerms().trim().split(",")));
			}
		}
		return permsSet;
	}
}